package me.chenjp.tracker.core.bean;

import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;

import java.io.Serializable;

/**
 * Created by gaopq on 2017/10/26.
 * Description:
 */
public class State implements Serializable {
    private Integer code;
    private String msg;

    public State() {
    }

    public State(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Integer getCode() {
        return this.code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public static State buildFromBindingResult(BindingResult bindingResult) {
        StringBuilder stringBuilder = new StringBuilder();
        bindingResult.getAllErrors().forEach((b) -> {
            stringBuilder.append(b.getDefaultMessage()).append("\n");
        });
        return new State(Integer.valueOf(HttpStatus.BAD_REQUEST.value() * 10), stringBuilder.toString());
    }
}
